package struts.ext.util;

import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import struts.ext.commons.lang.collection.Lists;


public abstract class Strings extends StringUtils {

	public static final String EMPTY = "";
	public static final String BLANK = " ";
	public static final String COMMA = ",";
	public static final String COLON = ":";
	
	/**
	 * 复制字符串
	 * 
	 * @param str 字符串
	 * @param num 数量
	 */
	public static String dup(String str, int num) {
		if (isEmpty(str) || num <= 0)
			return EMPTY;
		StringBuilder sb = new StringBuilder(str.length() * num);
		for (int i = 0; i < num; i++)
			sb.append(str);
		return sb.toString();
	}
	
	/**
	 * 检测输入的字符串是含有多个指定的字符串中的任何一个
	 * @param str 被检测的字符串
	 * @param subStrs 字符串集
	 * @return
	 */
	public static boolean containsAny(String str,String...subStrs) {
		if (isEmpty(str))
			return false;		
		for (String s:subStrs) {			
			if (contains(str,s))
				return true;
		}
		return false;
	}
	
	/**
	 * 检测输入的字符串是含有多个指定的字符串 
	 * @param str 被检测的字符串
	 * @param subStrs 字符串集
	 * @return
	 */
	public static boolean containsMany(String str,String...subStrs) {
		if (isEmpty(str))
			return false;		
		for (String s:subStrs) {			
			if (!contains(str,s))
				return false;
		}
		return true;
	}
	
	/**
	 * 检测输入的字符串是否符合指定的正则表达式
	 * @param rex 正则表达式
	 * @param str 输入的字符串
	 * @return
	 */
	public static boolean matches(String regex,String str) {
		if (isEmpty(regex))
			return true;
		return Pattern.matches(regex, str);
	}
	
	/**
	 * 根据指定的表达式查找和输入的字符串查找匹配的串
	 * @param regex 正则表达式
	 * @param str 被查找匹配的字符串
	 * @return 
	 */
	public static List<String> findMatches(String regex,String str) {
		Pattern p 				= Pattern.compile(regex);
		Matcher m 				= p.matcher(str);
		List<String> matches	= Lists.newList();
		while(m.find()) {
			matches.add(m.group());
		}
		return matches;
	}
	
	/**
	 * 检测某个字符串是否为空串，是就返回指定的默认串
	 * @param str 被检测的字符串
	 * @param l 默认串
	 * @return
	 */
	public static String nvl(String str,String l) {
		return defaultIfEmpty(str,l);
	}
	
	public static String nvl(String str){
		return defaultIfEmpty(str,EMPTY);
	}
	
	/**
	 * 将一个list用指定字符串连接成一个新的串,如
	 * <pre>
	 * List l = Lists.of("a","b","c","d");
	 * Strings.join(l,",")=[a,b,c,d]
	 * </pre>
	 * @param c
	 * @param joinStr
	 * @return
	 */
	public static String join(Collection<?> c,String joinStr) {
		return join(c.toArray(),joinStr);
	}
	
	/**
	 * @see Strings.join(Collection<?> c,String joinStr);
	 * @param objs
	 * @param joinStr
	 * @return
	 */
	public static String join(Object[] objs,String joinStr) {
		if (Objects.isEmpty(objs))
			return EMPTY;
		StringBuilder sb = new StringBuilder();
		for (int i = 0,len = objs.length; i < len; i++ ) {
			sb.append(Objects.toString(objs[i]));
			if (i < (len-1))
				sb.append(joinStr);
		}	
		return sb.toString();
	}
	
	
	/**
	 * 根据一个正则式，将字符串拆分成数组，空元素将被忽略
	 * 
	 * @param s 字符串
	 * @param regex  正则式
	 * @return 字符串数组
	 */
	public static String[] splitIgnoreBlank(String s, String regex) {
			return isEmpty(s)? 
					null:Arrays.removeIfEmpty(split(s,regex));		
	}
	
	public static String[] splitIgnoreBlank(String s) {
		return splitIgnoreBlank(s,Strings.COMMA);
	}
	
	public static Integer lengthBySplit(String str,String splitStr) {
		return Strings.split(str,splitStr).length;
	}
	
	public static void main(String[] args) throws Exception {
		
	}
	

}



